package com.fas.system.util;

import cn.hutool.core.util.RandomUtil;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.support.atomic.RedisAtomicLong;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Random;

@Component
@EnableCaching
@Log4j2

public class OrderldUtil {


    @Resource
    private RedisTemplate redisTemplate;

    //用作存放redis中的key
    private static String EQUIPMENT_KEY = "order_key";
    private static String IPQC_KEY = "ipqc_key";
    private static String FACILITY_KEY = "facilitys_key";
    private static String BULIDING_KEY="building_key";//建筑公司key

    /**
     * 订单id前缀
     */
    //设备编号
    private static final String EQUIPMENT_ID_PREFIX = "mhq";
    //    巡检员
    private static final String IPQC_ID_PREFIX = "yyxf";
    //设施编号
    private static final String FACILITY_ID_PREFIX = "bjan";
    //建筑编号前缀
    private static final String BULIDING_ID_PREFIX = "Ar";


    //生成特定的业务编号，prefix为特定的业务代码
    public String getIpqcNo() {
        return getSeqNo(IPQC_KEY, IPQC_ID_PREFIX);
    }

    public String getEquipmentNo() {
        return getSeqNo1(EQUIPMENT_KEY, EQUIPMENT_ID_PREFIX);
    }

    public String getFacilityNo() {
        return getSeqNo1(FACILITY_KEY, FACILITY_ID_PREFIX);
    }

    public String getBulidingNo() {
        return getSeqNo(BULIDING_KEY, BULIDING_ID_PREFIX);
    }

    //util类中公用部分，传入制定的key和prefix
    private String getSeqNo(String key, String prefix) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        //设置过期时间，这里设置为当天的23:59:59
        Date expireDate = calendar.getTime();
        //返回当前redis中的key的最大值
        Long seq = generate(redisTemplate, key, expireDate);
        //获取当天的日期，格式为yyyyMMdd
        String date = new SimpleDateFormat("yyyyMMdd").format(expireDate);
        //生成八为的序列号，如果seq不够八位，seq前面补0，
        //如果seq位数超过了八位，那么无需补0直接返回当前的seq
        String sequence = StringUtils.leftPad(seq.toString(), 8, "0");

        if (prefix == null) {
            prefix = "";
        }
        //拼接业务编号
        String seqNo = prefix + sequence;
        log.error("KEY:{}, 序列号生成:{}, 过期时间:{}", key, seqNo, String.format("%tF %tT ", expireDate, expireDate));
        return seqNo;
    }

    private String getSeqNo1(String key, String prefix) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        //设置过期时间，这里设置为当天的23:59:59
        Date expireDate = calendar.getTime();
        //返回当前redis中的key的最大值
        Long seq = generate(redisTemplate, key, expireDate);
        //获取当天的日期，格式为yyyyMMdd
        String date = new SimpleDateFormat("yyyyMMdd").format(expireDate);
        //生成八为的序列号，如果seq不够八位，seq前面补0，
        //如果seq位数超过了八位，那么无需补0直接返回当前的seq
        String sequence = StringUtils.leftPad(seq.toString(), 6, "0");

        if (prefix == null) {
            prefix = "";
        }
        //拼接业务编号
        String seqNo = prefix + sequence;
        log.error("KEY:{}, 序列号生成:{}, 过期时间:{}", key, seqNo, String.format("%tF %tT ", expireDate, expireDate));
        return seqNo;
    }


    /**
     * @param key
     * @param expireTime <i>过期时间</i>
     * @return
     */
    public static long generate(RedisTemplate<?, ?> redisTemplate, String key, Date expireTime) {
        //RedisAtomicLong为原子类，根据传入的key和redis链接工厂创建原子类
        RedisAtomicLong counter = new RedisAtomicLong(key, redisTemplate.getConnectionFactory());
        //设置过期时间
//        counter.expireAt(expireTime);
        //返回redis中key的值，内部实现下面详细说明
        return counter.incrementAndGet();
    }

    public static long randomNumber(){
        return RandomUtil.randomInt(100000000,999999999);
    }

    /**
     * 随机基数
     */
    private static final char[] charset = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'g', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};

    /**
     * 获取指定位数的随机字符串
     *
     * @param len 指定位数
     * @return
     */
    public static String getRandomStr(int len) {
        StringBuilder sb = new StringBuilder(len);
        for (int i = 0; i < len; i++) {
            char c = charset[new Random().nextInt(charset.length)];
            sb.append(c);
        }
        return sb.toString();
    }
}
